﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;

namespace SIPSSODemo
{
    class DemoMain
    {
        String api_server = "http://sip.alisoft.com/sip/rest" ;

        private String sip_appkey = "12562" ;
        
        private String sip_appsecret = "d48a15305a2211dda1dfd0ef302ff22c";

        static void Main(string[] args)
        {
            DemoMain main = new DemoMain();

            main.GetSubmitUrl(1);
        }


        private void GetSubmitUrl(int needParams)
        {
            String sip_apiname = "taobao.trades.bought.get";

            String sip_sessionid = "sddddeeefg-aef" ;

            String fields = "seller_nick,created";
            
            String start_created = "2008-01-07";
            
            String end_created = "2009-02-07";
            
            String page_no = "1";
            
            String page_size = "20";
            
            //String title = "a";
            
            //String status = "Close";

            String v = "1.0";

            String url = PrepareTaobaoTradesBoughtGet(sip_appkey, sip_apiname,
                    sip_appsecret, api_server, sip_sessionid, fields,
                    start_created, end_created, page_no, page_size,
                    v, needParams);

            Console.Out.WriteLine(url);
        }

        private String PrepareTaobaoTradesBoughtGet(String sip_appkey,
            String sip_apiname, String sip_appsecret, String api_server2,
            String sip_sessionid, String fields, String start_created,
            String end_created, String page_no, String page_size, 
            String v, int needparam)
        {
            String timestamp = GetTimestamp();

            StringBuilder urlStr = new StringBuilder(api_server);

            urlStr.Append(urlStr.ToString().IndexOf("?") != -1 ? "&" : "?");

            urlStr.Append("sip_timestamp=").Append(timestamp).Append("&");

            urlStr.Append("sip_appkey=").Append(sip_appkey).Append("&");
            
            urlStr.Append("sip_apiname=").Append(sip_apiname).Append("&");
            
            urlStr.Append("sip_sessionid=").Append(sip_sessionid).Append("&");

            urlStr.Append("fields=").Append(fields).Append("&");

            urlStr.Append("v=").Append(v).Append("&");

            urlStr.Append("start_created=").Append(start_created).Append("&");
            
            urlStr.Append("end_created=").Append(end_created).Append("&");
            
            urlStr.Append("page_no=").Append(page_no).Append("&");
            
            urlStr.Append("page_size=").Append(page_size).Append("&");
            
            String sip_isp_loginurl = "true";
            
            urlStr.Append("sip_isp_loginurl=").Append(sip_isp_loginurl).Append("&");

            SortedDictionary<string, string> map = new SortedDictionary<string, string>();

            map.Add("sip_timestamp", timestamp);

            map.Add("sip_appkey", sip_appkey);
            
            map.Add("sip_apiname", sip_apiname);
            
            map.Add("sip_sessionid", sip_sessionid);

            map.Add("fields", fields);

            map.Add("v", v);

            map.Add("start_created", start_created);
            
            map.Add("end_created", end_created);
            
            map.Add("page_no", page_no);
            
            map.Add("page_size", page_size);
            
            map.Add("sip_isp_loginurl", sip_isp_loginurl);

            String sign = SignatureUtil.Signature(map, sip_appsecret);

            urlStr.Append("sip_sign=").Append(sign);
            
            return urlStr.ToString().Replace(" ", "%20");
        }

        private String GetTimestamp()
        {
            return String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
        }

        private bool AndC(int a, int j)
        {
            int num = (int)(1 << j);

            if ((a & num) == num)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
